﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class OndutyCatController : Base
{
    private readonly OndutyCatService _ondutyCatService;
    public OndutyCatController(OndutyCatService ondutyCatService)
    {
        _ondutyCatService = ondutyCatService;
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Dict_Index)]
    public async Task<IActionResult> IndexAsync([FromQuery] QueryOndutyCat query)
    {
        PagedInfo<OndutyCat> pagedList = await _ondutyCatService.GetPageListAsync(query);
        return Success(new { count = pagedList.TotalNum, data = pagedList.Result });
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Onduty_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        OndutyCat data = await _ondutyCatService.GetIdAsync(id);
        return Success(new { data });
    }

    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.Onduty_AddOrEdit)]
    [Log(Name = "新增值班类别")]
    public async Task<IActionResult> CreatePostAsync([FromBody] OndutyCat onduty)
    {
        if (await _ondutyCatService.InsertAndDelCacheAsync(onduty) != null)
            return Success("新增值班类别: " + onduty.Name);
        else
            return Fail("失败");
    }
    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.Onduty_AddOrEdit)]
    [Log(Name = "修改值班类别")]
    public async Task<IActionResult> EditPostAsync([FromBody] OndutyCat onduty)
    {
        if (await _ondutyCatService.UpdateAndDelCacheAsync(onduty) > 0)
            return Success("成功修改值班类别：" + onduty.Name);
        else
            return Fail("失败，可能修改内容不存在");
    }
    [HttpDelete]
    [ActionPermissionFilter(Permission = PermissionKeys.Onduty_Delete)]
    [Log(Name = "删除值班类别")]
    public async Task<IActionResult> Delete(int id)
    {
        if (await _ondutyCatService.DeleteDelCache(id))
            return Success("删除值班类别 ID：" + id);
        else
            return Fail("删除失败,可能该类别下有信息");
    }
}
